<?php

namespace App\Http\Controllers\Admin;

use App\Common\Paginate;
use App\Common\Responses;
use App\Common\ValidatorCheck;
use App\Http\Controllers\Controller;
use App\Models\VrSellHouse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class VrSellHouseController extends Controller
{
    /*
     * 获取vr房源
     * @czy
     * */
    public function getIndex(Request $request){
        try {

            $params['page'] = (int)$request->get('page',1);
            $params['limit'] = (int)$request->get('limit',4);
            $params['house_name'] = (string)$request->get('k_house_name',"")?:"";
            $params['price'] = (float)$request->get('k_price',0);
            list($limit,$offset) = Paginate::paginate($params['page'],$params['limit']);
            $data = VrSellHouse::getSecharvr($params,$limit,$offset);
            return Responses::success($data,20000);
        }catch (\Exception $exception){
            return Responses::error([],2001,$exception->getMessage());
        }
    }
    /*
       * 获取vr房源详情
       * @czy
       * */
    public function getVrSellHouseDetail(Request $request)
    {

        try {
            $params['id'] = (int)$request->get('id')?:null;
            ValidatorCheck::paramsCheck($params,[
                'id' =>'required|integer',
            ]);
            $data =   VrSellHouse::detailVr([['is_del','=',0],['id','=',$params['id']]]);
            return Responses::success($data,20000);
        }catch (\Exception $exception){
            return Responses::error([$exception->getMessage()],$exception->getCode());
        }

    }

      /*
       * 添加vr房源详情
       * @czy
       * */
    public function createVrSellHouse(Request $request)
    {
        try {
            $params=$request->post('from');

            ValidatorCheck::paramsCheck($params,[
                'house_name' =>'required',
                'address' =>'required',
                'house_type'=>'required',
                'price'=>'required',
                'area'=>'required|numeric'
            ]);
            $data = VrSellHouse::createVr($params);
            return Responses::success($data,20000);
        }catch (\Exception $exception){

            return Responses::error([],$exception->getCode(),$exception->getMessage());
        }

    }


    /*
     * 修改vr房源详情
     * @czy
     * */
    public function updateVrSellHouse(Request $request)
    {
        try {
            $params=$request->post('from');
            ValidatorCheck::paramsCheck($params,[
                'house_name' =>'required',
                'address' =>'required',
                'house_type'=>'required',
                'developers' =>'required',
                'building_types'=>'required',
                'price'=>'required',
                'floor_plan' =>'required',
                'traffic_condition'=>'required',
                'area'=>'required|numeric',
            ]);
            $result = VrSellHouse::whereUpdateVr([['is_del','=','0'],['id','=',$params['id']]],$params);
            if (!$result) throw new \Exception("删除失败");
            return Responses::success($result,20000);
        }catch (\Exception $exception){

            return Responses::error([],$exception->getCode(),$exception->getMessage());
        }
    }


    /*
     * 删除vr房源
     * @czy
     * */
    public function delVrSellHouse(Request $request)
    {
        try {
            $params['id']=$request->get('id')?:null;
            ValidatorCheck::paramsCheck($params,[
                'id' =>'required|integer',
            ]);
            $data = VrSellHouse::whereUpdateVr([['is_del','=','0'],['id','=',$params['id']]],['is_del'=>1]);
            if (!$data)  return Responses::error($data,20000);

            return Responses::success($data,20000);
        }catch (\Exception $exception){

            return Responses::error([],$exception->getCode(),$exception->getMessage());
        }
    }



}
